package com.iOS.controller;

import com.iOS.dto.UserInfoExecution;
import com.iOS.entity.UserInfo;
import com.iOS.enums.UserInfoEnum;
import com.iOS.service.IUserInfoService;
import com.iOS.util.HttpServletRequestUtil;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping(value = "/userinfo", method = {RequestMethod.POST})
public class UserInfoController {
    @Autowired
    private IUserInfoService userInfoService;

    @RequestMapping(value = "/register")
    @ResponseBody
    public Map<String, Object> Register(HttpServletRequest request) {
        Map<String, Object> modelMap = new HashMap<String, Object>();
        String username = HttpServletRequestUtil.getString(request, "username");
        String password_1 = HttpServletRequestUtil.getString(request, "password_1");
        String password_2 = HttpServletRequestUtil.getString(request, "password_2");
        if (username.equals(null)||password_1.equals(null)||password_2.equals(null)){
            modelMap.put("success",false);
            modelMap.put("errMsg","请输入完整参数");
        }else {
            try {
                UserInfoExecution register = userInfoService.Register(username, password_1, password_2);
                if (register.getState() == UserInfoEnum.SUCCESS.getState()) {
                    modelMap.put("success", true);
                    modelMap.put("token", register.getLoginRecord().getToken());
                } else {
                    modelMap.put("success", false);
                    modelMap.put("errMsg", register.getStateInfo());
                }
            } catch (Exception e) {
                modelMap.put("success", false);
                modelMap.put("errMsg", e.getMessage());
            }
        }
        return modelMap;
    }

    @RequestMapping(value = "/login")
    @ResponseBody
    public Map<String, Object> Login(HttpServletRequest request) {
        Map<String, Object> modelMap = new HashMap<String, Object>();
        String username = HttpServletRequestUtil.getString(request, "username");
        String password = HttpServletRequestUtil.getString(request, "password");
        if (username.equals(null)||password.equals(null)){
            modelMap.put("success",false);
            modelMap.put("errMsg","请输入完整参数");
        }else {
            try {
                UserInfoExecution login = userInfoService.Login(username, password);
                if (login.getState() == UserInfoEnum.SUCCESS.getState()) {
                    modelMap.put("success", true);
                    modelMap.put("token", login.getLoginRecord().getToken());
                } else {
                    modelMap.put("success", false);
                    modelMap.put("errMsg", login.getStateInfo());
                }
            } catch (Exception e) {
                modelMap.put("success", false);
                modelMap.put("errMsg", e.getMessage());
            }
        }
        return modelMap;
    }

    @RequestMapping(value = "/show")
    @ResponseBody
    public Map<String, Object> Show(HttpServletRequest request) {
        Map<String, Object> modelMap = new HashMap<String, Object>();
        String token = HttpServletRequestUtil.getString(request, "token");
        if (token==null){
            modelMap.put("success",false);
            modelMap.put("errMsg","请输入完整信息");
        }else {
            try {
                UserInfoExecution show = userInfoService.GetUserInfo(token);
                if (show.getState() == UserInfoEnum.SUCCESS.getState()) {
                    modelMap.put("success", true);
                    modelMap.put("userInfo", show.getUserInfo());
                } else {
                    modelMap.put("success", false);
                    modelMap.put("errMsg", show.getStateInfo());
                }
            } catch (Exception e) {
                modelMap.put("success", false);
                modelMap.put("errMsg", e.getMessage());
            }
        }
        return modelMap;
    }


    @RequestMapping(value = "/complete")
    @ResponseBody
    public Map<String, Object> Complete(HttpServletRequest request) {
        Map<String, Object> modelMap = new HashMap<String, Object>();
        UserInfo userInfo = new UserInfo();
        userInfo.setNickname(HttpServletRequestUtil.getString(request,"nickname"));
        userInfo.setPassword(HttpServletRequestUtil.getString(request,"password"));
        userInfo.setPhone(HttpServletRequestUtil.getString(request,"phone"));
        userInfo.setPartType(HttpServletRequestUtil.getString(request,"parttype"));
        String token = HttpServletRequestUtil.getString(request,"token");
        /*获取图片*/
        MultipartResolver MultipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
        MultipartHttpServletRequest multipartHttpServletRequest =MultipartResolver.resolveMultipart(request);
        MultipartFile headImg = multipartHttpServletRequest.getFile("HeadImg");
        try {
            UserInfoExecution complete=userInfoService.CompleteUserInfo(userInfo,token,headImg.getInputStream(),headImg.getOriginalFilename());
            if (complete.getState()==UserInfoEnum.SUCCESS.getState()){
                modelMap.put("success",true);
                modelMap.put("userInfo",complete.getUserInfo());
            }else {
                modelMap.put("success",false);
                modelMap.put("errMsg",complete.getStateInfo());
            }
        }catch (Exception e){
            modelMap.put("success", false);
            modelMap.put("errMsg", e.getMessage());
        }
        return modelMap;
    }
}
